package com.lemner.motion.activity;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.lemner.motion.R;
import com.lemner.motion.base.BaseActivity;
import com.lemner.motion.base.BaseListener;
import com.lemner.motion.base.BaseModel;
import com.lemner.motion.bean.GetschoolBean;

import java.util.ArrayList;
import java.util.List;

public class GetSchoolActivity extends BaseActivity {
      private EditText et_search;
      private RecyclerView rv_school;
      private String stunum;
      private boolean schoolflag=false;

    @Override
    public int getLayoutFile() {
        return R.layout.activity_get_school;
    }

    @Override
    public void initView() {
        et_search=(EditText)findViewById(R.id.et_search);
        rv_school=(RecyclerView)findViewById(R.id.rv_school);
        LinearLayoutManager layoutManager=new LinearLayoutManager(this);
        rv_school.setLayoutManager(layoutManager);
    }

    @Override
    public void initEvent() {
        et_search.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                        getScholl();
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }

    @Override
    public void initData() {
        stunum=getIntent().getStringExtra("stunum");
        schoolflag=getIntent().getBooleanExtra("schoolflag",false);
    }

    @Override
    public void onClick(View v) {

    }

    BaseListener<GetschoolBean> listener=new BaseListener<GetschoolBean>() {
        @Override
        public void onResponse(GetschoolBean getschoolBean) {
            if (getschoolBean.getReturnCode().equals("0001")) {
                if (getschoolBean.getData() == null)
                    return;
                else {
                    List<GetschoolBean.DataBean> list=new ArrayList<GetschoolBean.DataBean>();
                    list.clear();
                    list.addAll(getschoolBean.getData());
                    if (list != null) {
                        Adapter adapter = new Adapter(list);
                        rv_school.setAdapter(adapter);
                    }
                }
            }
            else
                showToast("获取学校失败");
        }
        @Override
        public void onFail(String msg) {
            showToast(msg);
        }
    };

    public void getScholl(){
        BaseModel<GetschoolBean> model=new BaseModel<>();
        retrofit2.Call<GetschoolBean>call=model.service.getSchool(et_search.getText().toString());
        model.callEnqueue(call,listener);
    }

    class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder>{
             public List<GetschoolBean.DataBean>list;

        public Adapter(List<GetschoolBean.DataBean> list) {
            this.list = list;
            notifyDataSetChanged();
        }

        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.item_school,parent,false);
            ViewHolder holder=new ViewHolder(view);
            return holder ;
        }

        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
           final GetschoolBean.DataBean bean=list.get(position);
           if(bean!=null){
               holder.tv_school.setText(bean.getSchoolName());
               holder.tv_school.setOnClickListener(new View.OnClickListener() {
                   @Override
                   public void onClick(View view) {
                           Intent intent = new Intent(GetSchoolActivity.this, Register1Activity.class);
                           intent.putExtra("schoolname", bean.getSchoolName());
                           intent.putExtra("schoolid", bean.getSchoolId());
                           intent.putExtra("stunum", stunum);
                           startActivity(intent);
                           finish();

                   }
               });
           }
        }

        @Override
        public int getItemCount() {
            if(list==null)
                return 0;
            else
            return list.size();
        }

        public class ViewHolder extends RecyclerView.ViewHolder {
            TextView tv_school;
            public ViewHolder(View itemView) {
                super(itemView);
                tv_school=(TextView)itemView.findViewById(R.id.tv_school);
            }
        }
    }

}
